import javax.management.relation.RelationNotFoundException;

public class Demo5 {
    public static void main(String[] args) throws InterruptedException {
        Object object1=new Object();
        Object object2=new Object();
        Object object3=new Object();
        Thread t1=new Thread(()->{
            try{
                for (int i = 0; i < 10; i++) {
                    synchronized (object1){
                        object1.wait();
                    }
                    System.out.print("A");
                    synchronized (object2){
                        object2.notify();
                    }
                }
            }catch (InterruptedException e){
                throw new RuntimeException();
            }
        });
        Thread t2=new Thread(()->{
            try{
                for (int i = 0; i < 10; i++) {
                    synchronized (object2){
                        object2.wait();
                    }
                    System.out.print("B");
                    synchronized (object3){
                        object3.notify();
                    }
                }
            }catch (InterruptedException e){
                throw new RuntimeException();
            }
        });
        Thread t3=new Thread(()->{
            try{
                for (int i = 0; i < 10; i++) {
                    synchronized (object3){
                        object3.wait();
                    }
                    System.out.println("C");

                    synchronized (object1){
                        object1.notify();
                    }
                }
            }catch (InterruptedException e){
                throw new RuntimeException();
            }
        });


        t1.start();
        t2.start();
        t3.start();
        //先通知Object1先执行
        //须确保上述三个线程都执行到wait
        Thread.sleep(1000);
        synchronized (object1){
            object1.notify();
        }
    }
}
